<template>
	<view>
		<view class="search-box" >
			<uni-search-bar @confirm="search" @input="onInput" :radius="100" placeholder="请输入搜索内容" bgColor="white" cancelButton="none"></uni-search-bar>
		</view>
		
		<view class="sugg-list" v-if="searchResult.length!==0">
			<view class="sugg-item" v-for="(item,index) in searchResult" :key="index" @click="gotoDetail(item)">
				<view class="sugg-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		
		
		<view class="history-box" v-if="searchResult.length==0 && historyList.length>0"> 
				<view class="history-title">
					<text>搜索历史</text>
					<uni-icons class="rubbish-icon" type="trash-filled" size=30 @click="clearHistory"></uni-icons>
				</view>
				
				<view class="history-content">
					 <uni-tag v-for="(item,index) in histories" :text="item" :key="index" inverted=true @click="gotoGoodsList(item)"></uni-tag>
				</view>
		</view>
	</view>
</template> 

<script>
	export default {
		data() {
			return {
				timer:'',
				kw:'',
				searchResult:[],
				historyList:[],
			};
		},
		computed:{
			histories(){
				return [...this.historyList].reverse()
			}
		},
		methods:{
			onInput(e){
				clearTimeout(this.timer)
				this.timer=setTimeout(()=>{
						console.log(e);
						this.kw=e
						this.getSearchList()
						this.saveSearchHistory(this.kw)
				},500)
			},
			async getSearchList(){
				
				if(this.kw.trim()=='')return this.searchResult=[]
				
				const {data:res}=await uni.$http.get(`api/public/v1/goods/qsearch`,{query:this.kw})
				if(res.meta.status!=200) return uni.$showMsg()
				console.log(res);
				this.searchResult=res.message
				
			},
			gotoDetail(item){
					console.log(item.goods_id);
					uni.navigateTo({
						url:`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`
					})
			},	
			saveSearchHistory(kw){
					const set = new Set(this.historyList)
					set.delete(kw)
					set.add(kw)
					console.log(set);	
					this.historyList=Array.from(set)
					uni.setStorageSync('kw',JSON.stringify(this.historyList) )
			},
			clearHistory(){
				uni.setStorageSync('kw','[]') 
				this.historyList=[]
			},
			gotoGoodsList(kw){
				uni.navigateTo({
					url:`/subpkg/goods_list/goods_list?query=${kw}`
				})
			}
		},
		onLoad(){  
				this.historyList=JSON.parse(uni.getStorageSync('kw') || '[]') 
		}
	}
</script>

<style lang="scss">
.search-box{
	background-color: #FF0000;
	position: sticky;
	top: 0;
	z-index: 999;
}

.sugg-list{
	padding: 0 5px;
		.sugg-item{
			display: flex;
			justify-content: space-between;
			padding: 13px 0;
			font-size: 14px;
			border-bottom: solid 1px lightgray;
				.sugg-name{
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}
		}	
	}

.history-box{
	.history-title{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10px 5px;
		border-bottom: lightgray solid 1px;
		font-size: 16px;
	}
	.history-content{
		display: flex;	
		flex-wrap: wrap;
		padding-top: 10px;
		.uni-tag--default{
			font-size: 14px;
			font-weight: bolder;
		}
		.uni-tag--default--inverted{
			border:lightgray solid 1px;
			color: black;
		}
		.uni-tag{
			margin-top: 5px;
			margin-left: 5px;
		}
	}
}	
	
</style>
